<%--                                                                       --%>
<%-- Copyright (c) 2004 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<%@ include file="/mt/common/pickerPopupHelper.jsp" %>

<script language="JavaScript">

<%@ include file="/mc/common/jsutils.jsp" %>

  function doBlockEditAction(myform) {
    var id = myform.blockId.value;
    if (id == 0) {
      myform.blockId.value = <%= request.getAttribute("blockId") %>
    }
    myform.submit();
  }

  function doBlockDeleteAction(myform) {
    if (confirm('<bean-el:message key="ep.block.confirm.delete" bundle="ep.messages"/>') != true) {
      return;
    }
    var id = myform.blockId.value;
    if (id == 0) {
      myform.blockId.value = <%= request.getAttribute("blockId") %>
    }
    myform.goBackAction.value = '/ep/blockList.do';
    myform.action = '<c:url value="/ep/blockDelete.do"/>';
    myform.submit();
  }

  function doOneBlockProgramDeleteAction(oid) {
    document.epBlockEditForm.programId.value = oid;
    document.epBlockEditForm.action = '<c:url value="/ep/blockProgramDelete.do"/>';
    document.epBlockEditForm.submit();
  }

  function doBlockProgramDeleteAction() {
    document.epBlockEditForm.programId.value = 0;
    document.epBlockEditForm.action = '<c:url value="/ep/blockProgramDelete.do"/>';
    document.epBlockEditForm.submit();
  }
  
  function doBlockFindAndAddProgramAction(myform) {
    var formName = 'epBlockEditForm';
    var fieldName = 'selectedProgramIds';
    var thisForm = document.forms[formName];
    var elements = thisForm.elements;
    var numElements = elements.length;
    var field = null;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == fieldName) {
        field = elements[elementIndex];
        break;
      }
    }
    var selectedIds = '';
    if (field != null && field.length > 0) {
      for (var i = 0; i < field.length; i++) {
        selectedIds += field.options[i].value;
        if (i < field.length - 1) {
          selectedIds += ';';
        }
      }
    }
    doPickerPopupAction('<c:url value="/mt/pickerPopupMultiSelect.do"/>', 
      'setSelectedPrograms', 301, null, selectedIds, 'PROGRAM_NAME', true, true, true, 'com.broadvision.portal.tools.action.portlet.PortletProgramPickerFilter', null, 1);
  }

  function setSelectedPrograms(programs, programNames) {
    var formName = 'epBlockEditForm';
    var fieldName = 'selectedProgramIds';
    var thisForm = document.forms[formName];
    var elements = thisForm.elements;
    var numElements = elements.length;
    var field = null;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == fieldName) {
        field = elements[elementIndex];
        break;
      }
    }
    if (field != null) {
      var numOriginalPrograms = field.length;
      if (programs != null) {
        var numPrograms = programs.length;
        var i = 0;
        for (i = 0; i < numPrograms; i++) {
          if (i >= numOriginalPrograms) {
            var newoption = new Option(programs[i], programs[i], true, true);
            field.options[i] = newoption;
          }
          else {
            field.options[i].text = programs[i];
            field.options[i].value = programs[i];
            field.options[i].defaultSelected = true;
            field.options[i].selected = true;
          }
        }
        for (var j = numOriginalPrograms - 1; j >= i; j--) {
          field.options[j] = null;
        }
      }
      else {
        for (var i = numOriginalPrograms - 1; i >= 0; i--) {
          field.options[i] = null;
        }
      }
      var id = thisForm.blockId.value;
      if (id == 0) {
        thisForm.blockId.value = <%= request.getAttribute("blockId") %>
      }
      thisForm.action = '<c:url value="/ep/blockProgramListSave.do"/>';
      thisForm.submit();
    }
  }

</script>

<% 
  java.util.HashMap map = new java.util.HashMap();
  String ptOid = request.getParameter("pageTypeId"); 
  if (ptOid != null && ptOid.length() > 0) {
    map.put("pageTypeId", ptOid);
  }
  else {
    map.put("pageTypeId", "-1");
  }
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

<% String ctxPath = request.getContextPath(); %>
<c:set var="removeImg">
<img src="<%= ctxPath %>/ep/images/action/icon_remove.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.remove" bundle="ep.messages"/>" title="<bean-el:message key="image.alt.remove" bundle="ep.messages"/>">
</c:set>

<html-el:form action="/ep/blockEdit.do">
<input type="hidden" name="pageTypeId" value="<c:out value='${epPageTypeView.map.id}'/>"/>
<input type="hidden" name="blockId" value="<c:out value='${param.blockId}'/>"/>
<input type="hidden" name="goBackAction"/>
<input type="hidden" name="programId"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean-el:message key="ep.block.view.page.title" arg0="${epBlockView.map.name}" arg1="${epPageTypeView.map.name}" bundle="ep.messages"/>
      </div>
      <div class="errorMessage"><html-el:errors bundle="ep.messages"/></div>
      <%@ include file="/mc/common/pagetab.jsp" %>
      <div class="pageSubtitle">
        <span class="left">
          <bean-el:message key="ep.block.information" bundle="ep.messages"/>
        </span>
        <span class="right">
          <input type="button" class="inputButton" value='<bean-el:message key="button.edit" bundle="ep.messages"/>'
            onclick="doBlockEditAction(this.form);">
          <input type="button" class="inputButton" value='<bean-el:message key="button.delete" bundle="ep.messages"/>'
            onclick="doBlockDeleteAction(this.form);">
        </span>
      </div> 
      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.name" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <c:out value="${epBlockView.map.name}"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.display.template" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <c:out value="${epBlockView.map.displayTemplateName}"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.description" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <c:out value="${epBlockView.map.description}"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.type" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
<c:choose>
  <c:when test="${epBlockView.map.isSearchBlock == false && epBlockView.map.isNavigationBlock == false}">
    <c:if test="${epBlockView.map.blockType == 0}">
            <bean-el:message key="ep.block.type.optional" bundle="ep.messages"/>
    </c:if>
    <c:if test="${epBlockView.map.blockType == 1}">
            <bean-el:message key="ep.block.type.default" bundle="ep.messages"/>
    </c:if>
    <c:if test="${epBlockView.map.blockType == 2}">
            <bean-el:message key="ep.block.type.required" bundle="ep.messages"/>
    </c:if>
  </c:when>
  <c:otherwise>
    <c:if test="${epBlockView.map.isSearchBlock == true}">
            <bean-el:message key="ep.block.type.search" bundle="ep.messages"/>
    </c:if>
    <c:if test="${epBlockView.map.isNavigationBlock == true}">
            <bean-el:message key="ep.block.type.navigation" bundle="ep.messages"/>
    </c:if>
  </c:otherwise>
</c:choose> 
          </td>
        </tr>
<c:if test="${epBlockView.map.isPortletBlock == true}">
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.portlet.entity.name" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <c:out value="${epBlockView.map.portletEntityName}"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.block.portlet.entity.status" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
<c:choose>
  <c:when test="${epBlockView.map.portletEntityStatus == 0}">
            <bean-el:message key="ep.label.offline" bundle="ep.messages"/>
  </c:when>
  <c:otherwise>
	          <bean-el:message key="ep.label.online" bundle="ep.messages"/>
  </c:otherwise>
</c:choose>
          </td>
        </tr>
</c:if>
      </table>
<c:if test="${epBlockView.map.isSearchBlock == false && epBlockView.map.isNavigationBlock == false && epBlockView.map.isPortletBlock == false}">
      <div class="pageSubtitle">
        <span class="left"><bean-el:message key="ep.programs" bundle="ep.messages"/></span>
        <span class="right">
          <input type="button" class="inputButton" value='<bean-el:message key="button.programs.select" bundle="ep.messages"/>'
            onclick="doBlockFindAndAddProgramAction(this.form);">
        </span>
      </div> 
  <c:if test="${not empty list}">
      <div class="globalButtonWrap">
        <input type="button" name="actionSelectAll" class="globalButton" 
          onClick="check(this.form.programIds, this);"
          value="<bean-el:message key="button.selectAll" bundle="ep.messages"/>">
        <input type="button" name="actionDelete" class="globalButton" 
          onClick="if (isAnyChecked(this.form.programIds, '<bean-el:message key="ep.block.programs.alert.atLeastOne" bundle="ep.messages"/>', '')) doBlockProgramDeleteAction();" 
          value="<bean-el:message key="button.remove" bundle="ep.messages"/>">
      </div>
  </c:if>
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="programs table" width="100%">
        <tr class="listHeader">
          <th id="Checkboxes">&nbsp;</th>
          <th id="Name" align="left">
            <bean-el:message key="ep.header.name.program" bundle="ep.messages"/>
          </th>
          <th id="Status" align="left">
            <bean-el:message key="ep.header.status" bundle="ep.messages"/>
          </th>
          <th id="Actions" align="left" nowrap>
            <bean-el:message key="ep.header.actions" bundle="ep.messages"/>
          </th>
        </tr>
  <c:choose>
    <c:when test="${not empty list}">
      <c:forEach var="item" items="${list}" varStatus="status">
        <c:choose>
          <c:when test="${(status.count + 1) % 2 == 0}">
	      <tr class="listEven">
          </c:when> 
    	    <c:otherwise>
	      <tr class="listOdd">
	        </c:otherwise>
        </c:choose>
          <td headers="Checkboxes" align="center">
            <bean:define id="thisId" name="item" property="id"/>
            <input type="checkbox" name="programIds"
              onClick="update(this.form.programIds, this.form.actionSelectAll);"
              value="<bean:write name='thisId'/>"/>
          </td>
          <td headers="Name">
            <c:out value="${item.map.name}"/>
          </td>
          <td headers="Status">
        <c:choose>
          <c:when test="${item.map.status == 0}">
            <bean-el:message key="ep.label.offline" bundle="ep.messages"/>
          </c:when>
          <c:otherwise>
	          <bean-el:message key="ep.label.online" bundle="ep.messages"/>
          </c:otherwise>
        </c:choose>
          </td>
          <td headers="Actions">
            <a href='#' onclick="doOneBlockProgramDeleteAction('<c:out value="${item.map.id}"/>');">
              <c:out value='${removeImg}' escapeXml="false"/></a>
          </td>
        </tr>
      </c:forEach>
    </c:when>
    <c:otherwise>
        <tr class="listOdd">
          <td align="center" colspan="4">
            <bean-el:message key="ep.block.programs.noFound" bundle="ep.messages"/>
          </td>
        </tr>
    </c:otherwise>
  </c:choose>
      </table>
</c:if>
    </td> 
  </tr>
</table>
<select name="selectedProgramIds" multiple="multiple" size="0">
<c:choose>
  <c:when test="${not empty list}">
    <c:forEach var="item" items="${list}" varStatus="status">
  <option value="<c:out value='${item.map.id}'/>"><c:out value="${item.map.name}"/></option>
    </c:forEach>
  </c:when>
</c:choose>
</select>
<script language="Javascript1.2">
var theform = document.forms['epBlockEditForm'];
theform.selectedProgramIds.style.visibility = 'hidden';
theform.selectedProgramIds.style.display = 'none';
</script>
</html-el:form> 
